
%%%%%%%%%%%%%%%%%
% Return factor model, examine diversification effect,
% Add systematic and idiosyncratic risk estimates to the original factors
% Liuren Wu, liuren.wu@baruch.cuny.edu
%%%%%%%%%%%%%%%%%
nx=7;nf=nx+1;nlag=12;
minN=200;winth=0.01; 


dds=Z.dds;
ids=Z.ids;
dd=Z.dd; T=length(dd);
IV=mean(Z.IVV(:,1:2,1),2); %ATMV
y=mean(Z.NRV(:,1:2,1,3),2)*100; %straddle return, daily updated hedged
x=Z.Factors;%[hedging cost, vol risk, jump];
HVV=Z.HVV; %historical vol at 3 horizons



Bfv=NaN(T,nx);Afv=NaN(T,1);R2v=NaN(T,1);
statv=NaN(11,nx+2,T);acrr=NaN(T,1);
ccx=NaN(nx+2,nx+2,T);
VB=NaN(T,nx);
parfor t=nlag+1:T
    [Bi,nobt,Af,Bf,R2st,stats,ccxt,ccrt,crrt,vbt] =FunDailyORSestimationDivEffect(t,y,x,HVV,IV,ids,dds,dd,nlag,nx,winth,minN);
    
    Bfv(t,:)=Bf;   Afv(t)=Af;R2v(t)=R2st;
    ccx(:,:,t)=ccxt;
    VB(t,:)=vbt;
end


fl=[Afv,Bfv,100*R2v];

Tf=sum(isfinite(fl));
flstats=[nanmean(fl)', nanstd(fl,0)',sqrt(Tf)'.*nanmean(fl)'./nanneweystd(fl)',sqrt(12).*nanmean(fl)'./nanstd(fl)', nanacf(fl,1)', skewness(fl,0)', kurtosis(fl,0)'-3] ;

disp('Table 7. The pricing of systematic v idiosyncratic risk in delta hedged option investments');

xnamel={'Intercept','Systematic risk','Idiosyncratic risk', 'Hedging cost','Volatility risk','Jump risk','Historical premium','Volatility premium', ...
    'Adjusted $R^2$'}';

fprintf(1, '  \\\\ \n');
for j=[1,4:nf];fprintf(1, '%40s ', xnamel{j});fprintf(1, [repmat(' & %8.2f  ',1,7),' \\\\ \n'], flstats(j,:));end
fprintf(1, '  \\\\ \n');
for j=[2:3];fprintf(1, '%40s ', xnamel{j});fprintf(1, [repmat(' & %8.2f  ',1,7),' \\\\ \n'], flstats(j,:));end
fprintf(1, '  \\\\ \n');
for j=[nf+1];fprintf(1, '%40s ', xnamel{j});fprintf(1, [repmat(' & %8.2f  ',1,7),' \\\\ \n'], flstats(j,:));end



return

